home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / New System Software Extensions / QuickDraw™ GX v1.0ß2 / Sample Code / Printing Samples / Printer Drivers… / ImageWriter / ChooserSupport.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-13  |  6.9 KB  |  252 lines  |  [TEXT/MPS ]

  1. /*
  2.     ChooserSupport.c - C code for PACK and LDEF resources used by the Chooser.
  3.     
  4.     Copyright © 1992 Apple Computer, Inc.
  5.     All rights reserved.
  6.     
  7.     9/12/93        dmh        Modified for the b2 seed.
  8.     4/26/93        dmh        Modified for the b1 seed.
  9. */
  10.  
  11. #include <Types.h>
  12. #include <QuickDraw.h>
  13. #include <Fonts.h>
  14. #include <Lists.h>
  15. #include <Devices.h>
  16. #include <Resources.h>
  17. #include <Script.h>
  18. #include <ToolUtils.h>
  19. #include <SysEqu.h>
  20.  
  21. #include "graphics routines.h"
  22. #include <PrintingDrivers.h>
  23.  
  24. // ------------------------------------------------------------------------
  25. // INTERNAL DEFINES
  26. // ------------------------------------------------------------------------
  27. // Chooser initialize message selector
  28. #define initializeMsg    11
  29.  
  30. // Icon Suite support
  31. #define ttNone        0x0000
  32. #define ttDisabled    0x0001
  33. #define    ttOffline    0x0002
  34. #define ttOpen        0x0003
  35. #define ttSelected     0x4000
  36. #define ttSelectedDisabled    (ttSelected + ttDisabled)
  37. #define ttSelectedOffline    (ttSelected + ttOffline)
  38. #define ttSelectedOpen        (ttSelected + ttOpen)
  39.  
  40. #define ttLabel0    0x0000
  41. #define ttLabel1    0x0100
  42. #define ttLabel2    0x0200
  43. #define ttLabel3    0x0300
  44. #define ttLabel4    0x0400
  45. #define ttLabel5    0x0500
  46. #define ttLabel6    0x0600
  47. #define ttLabel7    0x0700
  48. pascal OSErr PlotIconSuite(const Rect * theRect, short align, short iconTransform, Handle cIcon)
  49.     = {0x303C, 0x0603, 0xABC9};
  50.  
  51. // Copy of the DrawText trap
  52. pascal void OldDrawText(const void *textBuf,short firstByte,short byteCount)
  53.     = 0xA885; 
  54.  
  55. // ------------------------------------------------------------------------
  56. // MAIN CODE FOR PACK
  57. // ------------------------------------------------------------------------
  58. pascal OSErr Device(short message, short caller, StringPtr objName, 
  59.     StringPtr zoneName, ListHandle theList, long p2)
  60. {
  61.     
  62.     OSErr            anErr = noErr;
  63.     extern Str31    gDriverName;
  64.     StringPtr        pDriverName = &gDriverName;
  65.     extern gxJob    gJob;
  66.     gxJob            *pJob = &gJob;
  67.     
  68.     // start up GX to begin with
  69.     if (message == initializeMsg)
  70.         {
  71.         FCBPBRec    pb;
  72.  
  73.         // determine the driver name
  74.         pb.ioCompletion     = nil;
  75.         pb.ioNamePtr         = pDriverName;
  76.         pb.ioVRefNum         = 0;
  77.         pb.ioRefNum         = CurResFile();
  78.         pb.ioFCBIndx         = 0;
  79.         anErr = PBGetFCBInfo(&pb, false);
  80.  
  81.         *pJob = nil;
  82.         if (anErr == noErr)
  83.             {
  84.             GXEnterGraphics();
  85.             anErr = GXInitPrinting();
  86.             if (anErr == noErr)
  87.                 anErr = GXNewJob(pJob);
  88.             }
  89.         }
  90.         
  91.     // let the system handle the choosing for us (how nice of it!)
  92.     if (anErr == noErr)
  93.         anErr = GXHandleChooserMessage(pJob, pDriverName, message, caller, objName, zoneName, theList, p2);
  94.         
  95.     // tear down GX when done
  96.     if ( (message == terminateMsg) && (p2 == terminateMsg) )
  97.         {
  98.         if (*pJob != nil)
  99.             GXDisposeJob(*pJob);
  100.         GXExitPrinting();
  101.         GXExitGraphics();
  102.         }
  103.  
  104.     return(anErr);
  105.     
  106. } // Device
  107.  
  108.  
  109.  
  110. // ------------------------------------------------------------------------
  111. // ENTRY POINT FOR LDEF
  112. // ------------------------------------------------------------------------
  113.  
  114. pascal void LDEF(
  115.     short         message,        // What operation to perform on list
  116.     Boolean     select,            // Is this cell to be selected or not?
  117.     Rect        *theRect,        // Rectangle of this cell, clipped to window
  118.     Cell        theCell,        // Which cell this is
  119.     short        dataOffset,        // Offset into data for this cell
  120.     short        dataLen,        // Length of data for this cell
  121.     ListHandle    theList)        // The list to act upon
  122. /*
  123.     An LDEF that works in two modes:
  124.         - if the first two characters of the cell are valid AppleTalk NBP names (ie, not ≈ ≈)
  125.           then the LDEF is just a basic text LDEF
  126.         - otherwise, it assumes the data is part of a PortListRec, which is
  127.           a structure for icons with text underneath
  128.           
  129.     Things not yet done:
  130.         - truncate/compress for text strings
  131.         - speed improvements w.r.t. draw & hilite
  132. */
  133.  
  134. {
  135. #pragma unused (theCell, dataLen)
  136.  
  137.     gxPortListRec    theCellContents;
  138.     Rect            iconRect;
  139.     
  140.     switch (message)
  141.         {
  142.         case lDrawMsg:
  143.         case lHiliteMsg:
  144.         
  145.             // save the data to avoid locking things down
  146.             if (dataLen > sizeof(theCellContents) )
  147.                 dataLen = sizeof(theCellContents);
  148.             BlockMove(((*(**theList).cells) + dataOffset), &theCellContents, dataLen );
  149.             
  150.             // draw the cell as an icon, but only if we see our magic marker at the front
  151.             if ( (theCellContents.firstMarker == '≈') && (theCellContents.secondMarker == '≈') )
  152.                 {
  153.                 // center the icon rect on the list with a top margin of 10 pixels
  154.                 iconRect.top = theRect->top + 10;
  155.                 iconRect.left = theRect->left + ((theRect->right - theRect->left) >> 1) - 16;
  156.                 iconRect.bottom = iconRect.top + 32;
  157.                 iconRect.right = iconRect.left + 32;
  158.                 
  159.                 
  160.                 // draw the icon
  161.                 if (theCellContents.iconSuiteHandle != nil)
  162.                     PlotIconSuite(&iconRect,
  163.                             ttNone, (select) ? ttSelected: ttNone,
  164.                             theCellContents.iconSuiteHandle);
  165.                             
  166.                 // Get the general area under the icon in which to draw the label
  167.                 iconRect.left = theRect->left + 2;
  168.                 iconRect.right = iconRect.left + (**theList).cellSize.h - 2;
  169.                 iconRect.top = iconRect.bottom + 2;
  170.                 iconRect.bottom = theRect->bottom;
  171.     
  172.                 // use a nice small font for the label            
  173.                 TextFont(applFont);
  174.                 TextSize(9);
  175.                 
  176.                     {
  177.                     short        labelWidth;
  178.                     short        rectWidth;
  179.                     short        labelHeight;
  180.                     FontInfo    theInfo;
  181.                 
  182.                     // Get rid of any text that was there before
  183.                     EraseRect(&iconRect);
  184.                     iconRect.top += 2;
  185.                     
  186.                     // compute the height of the label                    
  187.                     GetFontInfo(&theInfo);
  188.                     labelHeight = theInfo.ascent + theInfo.leading;
  189.                     
  190.                     // compute where to draw the text
  191.                     iconRect.bottom = iconRect.top + labelHeight;
  192.                     rectWidth = iconRect.right-iconRect.left;
  193.                     
  194.                     // truncate the string to fit within the box
  195.                     TruncString(rectWidth, theCellContents.iconName, smTruncEnd);
  196.                     
  197.                     // compute the new width of the string
  198.                     labelWidth = StringWidth(theCellContents.iconName);
  199.                     
  200.                     // center the string, draw it
  201.                     iconRect.left += (rectWidth >> 1) - (labelWidth >> 1);
  202.                     MoveTo(iconRect.left, iconRect.bottom);
  203.                     DrawString(theCellContents.iconName);
  204.                     
  205.                     if (select)
  206.                         {
  207.                         // compute right and lower edge of box bounding the text we just drew
  208.                         iconRect.right = iconRect.left + labelWidth;
  209.                         iconRect.bottom += theInfo.descent;
  210.                         
  211.                         // outset it, and invert it to select it
  212.                         InsetRect(&iconRect, -1, -1);
  213.                         BitClr(pHiliteBit, HiliteMode);
  214.                         InvertRect(&iconRect);
  215.                         }
  216.                     }
  217.                     
  218.                 TextFont(applFont);
  219.                 TextSize(0);
  220.                 }
  221.             else
  222.                 {
  223.                 // how boring!  It's only text
  224.                 FontInfo    theInfo;
  225.                 Rect        ourRect;
  226.                 
  227.                 // add a margin to the rectangle
  228.                 ourRect = *theRect;
  229.                 ourRect.left += 3;
  230.                 
  231.                 // erase the rectangle
  232.                 GetFontInfo(&theInfo);
  233.                 EraseRect(theRect);
  234.                 MoveTo(ourRect.left, ourRect.bottom - theInfo.descent);
  235.                                 
  236.                 // those darn other languages!
  237.                 if (GetSysJust() == teJustRight)
  238.                     Move(ourRect.right-ourRect.left-TextWidth((Ptr) &theCellContents, 0, dataLen) , 0);
  239.                     
  240.                 OldDrawText((Ptr) &theCellContents, 0, dataLen);
  241.                 
  242.                 // if selected, invert it
  243.                 if (select)
  244.                     InvertRect(theRect);
  245.                 }
  246.                 
  247.             break;
  248.             
  249.         } // switch
  250.         
  251. } // LDEF
  252.